% codes to construct database with the data generated when students make
% mistakes.

% 1) sample_id: sample_id
var.sample_id = repmat([1:M],I*J,P);
var.sample_id = var.sample_id(:);
% updated later (at the bottom)

% 2) stu_id: student id
var.stu_id = repmat([1:I],J,M*P);
var.stu_id = var.stu_id(:);

% 3) sch_id: school id
var.sch_id = repmat([1:J]',M*P*I,1);

% 4) sch_true_rank: true ranking of the school
% TT.ROL (I,J,M)->(I,M,J)
var.sch_true_rank_long = permute(repmat(TT.ROL,1,1,P), [1 3 2]);
var.sch_true_rank_long = reshape(var.sch_true_rank_long,M*P*I,J);
[row,col,val]=find(var.sch_true_rank_long);
var.sch_true_rank_long = full(sparse(row,val,col));
var.sch_true_rank = reshape(var.sch_true_rank_long',1,[])';
clear row col val;

% 5) rank: submitted rank of school (0 = not listed / 1 = first choice, 2 = second choice, etc.)
% DStruct.ROL (I,J,M,P)->(I,M*P,J)
var.sch_sub_rank_long = reshape(DStruct.ROL,I,J,M*P);
var.sch_sub_rank_long = permute(var.sch_sub_rank_long,[1 3 2]);
var.sch_sub_rank_long = reshape(var.sch_sub_rank_long,M*P*I,J);
[row,col,val]=find(var.sch_sub_rank_long);
var.sch_sub_rank_long = full(sparse(row,val,col));
var.sch_sub_rank = reshape(var.sch_sub_rank_long',1,[])';
clear row col val;

% 6) stu_epri: student's expected priority
% MC.Priority (I,1)->(J,I,M*P)->(I,M*P,J)
var.stu_eprio_long = repmat(MC.Priority,1,M*P,J);
var.stu_eprio_long = reshape(var.stu_eprio_long,M*P*I,J);
var.stu_eprio = reshape(var.stu_eprio_long',1,[])';

% 7) stu_prio: student's observed priority for each school (changes across samples)
% var.stu_prio_long = permute(MC.Priorities_Observed,[2 3 1]);
% var.stu_prio_long = reshape(var.stu_prio_long,M*I,J);
var.stu_prio_long = var.stu_eprio_long;
var.stu_prio = var.stu_eprio; %reshape(var.stu_prio_long',1,[])';
 
% 8) sch_cutoff: school's admission cutoff
% DStruct.Cutoffs (J,M,P)->(J,M*P,I)->(I,M*P,J)
var.sch_cutoff_long = reshape(DStruct.Cutoffs,[J,M*P]);
var.sch_cutoff_long = repmat(var.sch_cutoff_long,1,1,I);
var.sch_cutoff_long = permute(var.sch_cutoff_long,[3 2 1]);
var.sch_cutoff_long = reshape(var.sch_cutoff_long,M*P*I,J);
var.sch_cutoff = reshape(var.sch_cutoff_long',1,[])';

% 9) sch_feas: school is feasible for the student
var.sch_feas_long = (var.stu_prio_long >= var.sch_cutoff_long);
var.sch_feas = reshape(var.sch_feas_long',1,[])';

% 10) sch_assignment: student is assigned to that school
% DStruct.Assigned: M by I by P
var.sch_assignment_long = reshape(permute(DStruct.Assigned,[1,3,2]),[M*P,I])';
var.sch_assignment_long = var.sch_assignment_long(:);
[row,col,val]=find(var.sch_assignment_long);
var.sch_assignment_long = full(sparse(row,val,col));
var.sch_assignment = reshape(var.sch_assignment_long',1,[])';
clear row col val;
   
% 11) id for fraction of mistakes
var.pp_mistake = repmat(reshape((1:P),[1,1,P]),I*J,M,1);
var.pp_mistake = var.pp_mistake(:);
% updated later (at the bottom)


% 12) sch_dist: Distance to school
var.sch_dist_long = repmat(MC.distance_school,1,1,P);
var.sch_dist_long = permute(var.sch_dist_long, [1 3 2]);
var.sch_dist_long = reshape(var.sch_dist_long,M*P*I,J);
var.sch_dist = reshape(var.sch_dist_long',1,[])';

% 13) nb_ranked: Number of ranked schools
var.nb_sch_ranked_long = reshape(DStruct.ROL,[I,J,M*P]);
var.nb_sch_ranked_long = permute(var.nb_sch_ranked_long,[1 3 2]);
var.nb_sch_ranked_long = reshape(var.nb_sch_ranked_long,M*P*I,J);
var.nb_sch_ranked_long = sum(var.nb_sch_ranked_long~=0,2);
var.nb_sch_ranked = reshape(repmat(var.nb_sch_ranked_long,1,J)',1,[])';

% 14) sch_true_rank_feas: true ranking of the school among ex post feasible
var.sch_true_rank_feas_long = var.sch_true_rank_long .* var.sch_feas_long;
var.sch_true_rank_feas_long(var.sch_true_rank_feas_long==0)=NaN;
[~, var.sch_true_rank_feas_long] = sort(var.sch_true_rank_feas_long,2);
[A1, A2]=ndgrid( 1:size(var.sch_true_rank_feas_long,1), 1:size(var.sch_true_rank_feas_long,2) );
var.sch_true_rank_feas_long(sub2ind(size(var.sch_true_rank_feas_long), A1, var.sch_true_rank_feas_long)) = A2;
var.sch_true_rank_feas_long = var.sch_true_rank_feas_long .* var.sch_feas_long;
var.sch_true_rank_feas = reshape(var.sch_true_rank_feas_long',1,[])';
clear A1 A2;

% 15) sch_sub_rank_feas: submitted rank among feasible
var.sch_sub_rank_feas_long = var.sch_sub_rank_long .* var.sch_feas_long;
var.sch_sub_rank_feas_long(var.sch_sub_rank_feas_long==0)=NaN;
[~, var.sch_sub_rank_feas_long] = sort(var.sch_sub_rank_feas_long,2);
[A1, A2]=ndgrid( 1:size(var.sch_sub_rank_feas_long,1), 1:size(var.sch_sub_rank_feas_long,2) );
var.sch_sub_rank_feas_long(sub2ind(size(var.sch_sub_rank_feas_long), A1, var.sch_sub_rank_feas_long)) = A2;
var.sch_sub_rank_feas_long = var.sch_sub_rank_feas_long .* (var.sch_sub_rank_long>0) .* (var.sch_feas_long>0);
var.sch_sub_rank_feas = reshape(var.sch_sub_rank_feas_long',1,[])';
clear A1 A2;

% 16) stu_assigned: Student is assigned to a school
var.stu_assigned_long = sum(var.sch_assignment_long~=0,2);
var.stu_assigned = reshape(repmat(var.stu_assigned_long,1,J)',1,[])';

% 17) stu_assigned_pref_feas: Student is assigned to most preferred out of ex post feasible
var.sch_pref_feas_long = (var.sch_true_rank_feas_long==1);
var.stu_assigned_pref_feas_long = sum((var.sch_assignment_long == var.sch_pref_feas_long),2)==J;
var.stu_assigned_pref_feas = reshape(repmat(var.stu_assigned_pref_feas_long,1,J)',1,[])';

% 18) stu_truthful_feas: Ex post feasible are ranked according to true preferences
var.stu_truthful_feas_long = sum( (var.sch_true_rank_feas_long .* (var.sch_sub_rank_feas_long>0)) == var.sch_sub_rank_feas_long, 2) == J;
var.stu_truthful_feas = reshape(repmat(var.stu_truthful_feas_long,1,J)',1,[])';

% 19) stu_assigned_pref: Student is assigned to most preferred school
var.sch_pref_long = (var.sch_true_rank_long==1);
var.stu_assigned_pref_long = sum((var.sch_assignment_long == var.sch_pref_long),2)==J;
var.stu_assigned_pref = reshape(repmat(var.stu_assigned_pref_long,1,J)',1,[])';

% 20) stu_topk: Student has ranked top k most preferred schools
var.stu_topk_long = sum( (var.sch_true_rank_long .* (var.sch_sub_rank_long>0)) == var.sch_sub_rank_long, 2) == J;
var.stu_topk = reshape(repmat(var.stu_topk_long,1,J)',1,[])';

% 21) stu_top1: Student has ranked most preferred school first
var.sch_rk1_long = (var.sch_sub_rank_long==1);
var.stu_top1_long = sum(var.sch_rk1_long == var.sch_pref_long, 2) == J;
var.stu_top1 = reshape(repmat(var.stu_top1_long,1,J)',1,[])';

% 22) sch_assignment_unconstrained: student's assignment w/o mistake
% TT.Assigned: M by I
var.sch_assignment_TT_long = repmat(TT.Assigned,P,1)';
var.sch_assignment_TT_long = var.sch_assignment_TT_long(:);
[row,col,val]=find(var.sch_assignment_TT_long);
var.sch_assignment_TT_long = full(sparse(row,val,col));
var.sch_assignment_unconstrained = reshape(var.sch_assignment_TT_long',1,[])';
clear row col val;

% 23) Student scores
% % (Ix1) -> (J,I) -> (J,I,M) -> (I,M,J)
% var.Stu_char_long = repmat(repmat(MC.Stu_char,1,J)', [1,1,M*P]);
% var.Stu_char_long = permute(var.Stu_char_long,[2 3 1]);
% var.Stu_char_long = reshape(var.Stu_char_long,M*P*I,J);
% var.Stu_char = reshape(var.Stu_char_long',1,[])';
% (Ix1) -> (J,I) -> (J,I,M) -> (I,M,J)
var.Stu_char_long = repmat(MC.Stu_char,1,P,J);
var.Stu_char_long = reshape(var.Stu_char_long,M*P*I,J);
var.Stu_char = reshape(var.Stu_char_long',1,[])';


% 11) id for fraction of mistakes -- last two digits;
% other digits -- simulation sample
var.pp_mistake = var.pp_mistake + var.sample_id*100;

% 1) sample_id: sample_id, "contituous"
var.sample_id = repmat([1:M*P],I*J,1);
var.sample_id = var.sample_id(:);